﻿using MailDAL;
using MailTool.Utility;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MailTool.Pages
{
    public partial class MailAddressList : System.Web.UI.Page
    {
        UnitOfWork unitOfWork = new UnitOfWork();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.BindData();
            }
        }

        public void BindData()
        {
            int size = this.Pagination.PageSize;
            int pageIndex = this.Pagination.PageIndex;
            pageIndex = pageIndex == 0 ? 0 : pageIndex - 1;
            int recordCount = unitOfWork.AddressListRepository.Get(p => p.OwnerId == SessionState.CurrentUser.Id).Count();

            var list = unitOfWork.AddressListRepository.Get(p => p.OwnerId == SessionState.CurrentUser.Id, p => p.OrderBy(x => x.UserName), size, size * pageIndex, p => p.User).ToList();

            this.rptNewsList.DataSource = list;
            this.rptNewsList.DataBind();
            this.Pagination.BindName = "BindData";
            this.Pagination.Count = recordCount;
            this.Pagination.BindPager();
        }

        protected void DeleteNewsByLink(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(((LinkButton)sender).CommandArgument.ToString());

            try
            {
                MailModel.AddressList template = unitOfWork.AddressListRepository.Get(p => p.Id == id).FirstOrDefault();
                if (template != null)
                {
                    unitOfWork.AddressListRepository.Delete(template);
                    unitOfWork.Save();
                    this.BindData();
                    JsUtil.Alert("删除成功！", this.Page);
                }
                else
                {
                    JsUtil.Alert("删除失败！", this.Page);
                }
            }
            catch
            {
                JsUtil.Alert("删除失败！", this.Page);
            }
        }

        protected void BenDeleteSelect(object sender, EventArgs e)
        {
            ArrayList ids = new ArrayList();
            foreach (RepeaterItem item in this.rptNewsList.Items)
            {
                CheckBox CheckboxSign = item.FindControl("ChkSelect") as CheckBox;
                if (CheckboxSign.Checked == true)
                {
                    ids.Add(((HiddenField)this.rptNewsList.Items[item.ItemIndex].FindControl("HiddenId")).Value);
                }
            }
            if (ids.Count > 0)
            {
                try
                {
                    Int32[] id_list = ids.Cast<object>().Select(t => Convert.ToInt32(t)).ToArray();

                    if (unitOfWork.AddressListRepository.Get(p => id_list.Contains(p.Id) == true).Select(p => p.Id).Count() > 0)
                    {
                        unitOfWork.AddressListRepository.Delete(p => id_list.Contains(p.Id) == true);
                        unitOfWork.Save();
                        this.BindData();
                        JsUtil.Alert("删除成功！", this.Page);
                    }
                    else
                    {
                        JsUtil.Alert("删除失败！", this.Page);
                    }
                }
                catch
                {
                    JsUtil.Alert("删除失败！", this.Page);
                }
            }
            else
            {
                JsUtil.Alert("请选择需要删除的记录！", this.Page);
            }
        }

        protected void BtnSearchRecord_Click(object sender, EventArgs e)
        {
            string search = txtSearch.Text.Trim();

            int size = this.Pagination.PageSize;
            int pageIndex = this.Pagination.PageIndex;
            pageIndex = pageIndex == 0 ? 0 : pageIndex - 1;
            int recordCount = unitOfWork.AddressListRepository.Get(p => p.UserName.Contains(search) || p.Address.Contains(search) || p.Tag.Contains(search)).Count();

            var list = unitOfWork.AddressListRepository.Get(p => p.UserName.Contains(search) || p.Address.Contains(search) || p.Tag.Contains(search),
                    p => p.OrderBy(x => x.UserName), 
                    size, 
                    size * pageIndex, 
                    p => p.User
                ).ToList();

            this.rptNewsList.DataSource = list;
            this.rptNewsList.DataBind();
            this.Pagination.BindName = "BindData";
            this.Pagination.Count = recordCount;
            this.Pagination.BindPager();
        }
    }
}